package com.gouwudai.gobbs.common;

import java.sql.Connection;
import java.sql.SQLException;

import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.dbcp.ConnectionFactory;
import org.apache.commons.dbcp.DataSourceConnectionFactory;

import com.litt.core.common.XMLConfig;
import com.litt.core.exception.BusiException;

/**
 * 
 * <pre>
 *  Title:数据库直连工具类(仅用于测试，正式系统要使用容器管理的连接池)
 *  Description: 从XML配置文件中直接读取  
 * </pre>
 * 
 * @author <a href="mailto:littcai@hotmail.com">空心大白菜</a>
 * @date 2007-4-1
 * @version 1.0
 * @see com.litt.core.common.XMLConfig
 * <pre>
 * 修改记录
 *    修改后版本:     修改人：  修改日期:     修改内容: 
 * </pre>
 */
public class DBManager 
{
	private static BasicDataSource bds = null;
	private static ConnectionFactory fac = null;

	public static void init() throws BusiException
	{
		//初始化数据库连接池
		bds = new BasicDataSource();
		bds.setDriverClassName(XMLConfig.getValue("jdbc.driverClassName")); 
		bds.setUrl(XMLConfig.getValue("jdbc.url"));
		bds.setUsername(XMLConfig.getValue("jdbc.username"));
		bds.setPassword(XMLConfig.getValue("jdbc.password"));
	}
	
	public static Connection getConnection() throws SQLException,BusiException
	{
		if(bds==null)
			init();
		if(fac==null)
			fac = new DataSourceConnectionFactory(bds);
		Connection conn = fac.createConnection();          	
		return conn;
	}

	public void destory() throws SQLException
	{		
		bds.close();
		bds = null;
	}

}
